package com.zzb.framework.common;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 基本分页
 * Created by zhangzhenbin on 2016/12/26.
 */
public class BasePage<T> implements Serializable {
    public final static int SUCCESS = 1;
    public final static int FAIL = 0;
    public final static int UNLOGIN = -1;

    public BasePage(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public BasePage() {
    }


    /**
     * 分页
     */
    public BasePage(IPage<T> ipage) {
        if(ipage.getRecords() == null){
            this.status = FAIL;
            this.msg = "获取分页失败，请联系管理员";
        } else {
            this.status = SUCCESS;
            this.msg = "获取分页成功";
            this.content = ipage.getRecords();
            this.totalElements = (int) ipage.getTotal();
            this.pageSize = (int) ipage.getSize();
            this.page = (int) ipage.getCurrent();
            this.totalPages = (int) ipage.getPages();
            if(this.page < 1)this.page = 1;
            this.offset = (page-1) * pageSize + 1;
            this.lastPage = page == this.totalPages;
        }
    }

    public BasePage(List<T> content ,int page ,int pageSize ,long total){
        if(content == null){
            this.status = FAIL;
            this.msg = "获取分页失败，请联系管理员";
        } else {
            if(page < 1)page = 1;
            this.status = SUCCESS;
            this.msg = "获取分页成功";
            this.content = content;
            this.page = page;
            this.pageSize = pageSize;
            this.firstPage = page == 1;
            if( pageSize > 0 ) this.totalPages =  new BigDecimal((double) total / pageSize).setScale(0, RoundingMode.UP).intValue() ;
            else this.totalPages = 1;
            this.lastPage = page == this.totalPages;
            this.totalElements = total;
            this.offset = (page-1) * pageSize + 1;
        }
    }


    @ApiModelProperty(value = "返回状态，1成功，0失败，-1未登录")
    private int status;
    @ApiModelProperty(value = "返回消息")
    private String msg;
    @ApiModelProperty(value = "返回内容")
    private List<T> content;
    @ApiModelProperty(value = "当前页数 从1开始")
    private int page;
    @ApiModelProperty(value = "分页大小")
    private int pageSize;
    @ApiModelProperty(value = "总页数")
    private long totalPages;
    @ApiModelProperty(value = "总记录数")
    private long totalElements;
    @ApiModelProperty(value = "是否是第一页")
    private boolean firstPage;
    @ApiModelProperty(value = "是否是最后一页")
    private boolean lastPage;
    @ApiModelProperty(value = "分页位移量")
    private long offset;

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(long totalPages) {
        this.totalPages = totalPages;
    }

    public long getTotalElements() {
        return totalElements;
    }

    public void setTotalElements(long totalElements) {
        this.totalElements = totalElements;
    }

    public boolean isFirstPage() {
        return firstPage;
    }

    public void setFirstPage(boolean firstPage) {
        this.firstPage = firstPage;
    }

    public boolean isLastPage() {
        return lastPage;
    }

    public void setLastPage(boolean lastPage) {
        this.lastPage = lastPage;
    }

    public long getOffset() {
        return offset;
    }

    public void setOffset(long offset) {
        this.offset = offset;
    }
}
